//
//  DataProvider.swift
//  CMCCIOTSwift
//
//  Created by isoftstone on 2017/4/20.
//  Copyright © 2017年 isoftstone. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON
public class DataProvider<Target:RequestBaseProtocol>{
    
}
extension DataProvider{
    
    public func requestJSON(target : Target,returnVal : @escaping(_ retureVal : [String:Any] )->Void,failur : @escaping(_ faliur : Error? ) -> Void) -> Void{
        
       
        let request  = RequestEntity<Target>().request(target: target)
        request.responseJSON { (response) in
        
           if(response.result.isSuccess)
           {
             if let value = response.result.value{
             debugPrintLog("responseSuccess:\(value)")
             let jsonData = JSON(value)
              
             debugPrintLog("responseSuccessJson:\(jsonData)")
             returnVal(jsonData.dictionaryObject!)
            }
            
            }
           else{
              debugPrintLog("responseFailur:\(String(describing: response.error))")
              failur(response.error)
            
            }
        }
    }
    
    public func requestData(target : Target,returnVal : @escaping(_ retureVal : [String:Any] )->Void,failur : @escaping(_ faliur : Error? ) -> Void) -> Void{
        let request  = RequestEntity<Target>().request(target: target)
        request.responseData { (response) in
            
            if(response.result.isSuccess)
            {
                if let value = response.result.value{
                    debugPrintLog("responseSuccess:\(value)")
                    
                    let dictionary:NSDictionary = NSKeyedUnarchiver.unarchiveObject(with: value)! as! NSDictionary
                    returnVal(dictionary as! [String : Any])

                }
                
            }
            else{
                debugPrintLog("responseFailur:\(String(describing: response.error))")
                failur(response.error)
                
            }
        }

    }
    
    
}
